package com.foreverwin.jzymes.report.gr.service.impl;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.production.gr.domain.GoodsReceiptLog;
import com.foreverwin.jzymes.report.gr.mapper.GoodsReceiptReportMapper;
import com.foreverwin.jzymes.report.gr.service.IGoodsReceiptReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class GoodsReceiptReportServiceImpl implements IGoodsReceiptReportService {
    @Autowired
    GoodsReceiptReportMapper adviceMapper;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;

    /**成品入库报表
     *
     * @param jsonMap
     * @return
     *
     * */
    @Override
    public List<Map<String , Object >> findGoodsReceiptReport(Map<String, Object> jsonMap) {
        String site = (String)jsonMap.get("site");
        String itemBo = (String)jsonMap.get("itemBo");
        String sfc = (String)jsonMap.get("sfc");

        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if ( (String) jsonMap.get( "startDate" ) != "" ){
            try {
                startDate = simple.parse( (String) jsonMap.get( "startDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if ( (String) jsonMap.get( "startDate" ) != "" ){
            try {
                endDate = simple.parse( (String) jsonMap.get( "endDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        List<GoodsReceiptLog> goodsReceiptLogs =  adviceMapper.selectGoodsReceiptReport( site,itemBo,sfc,startDate,endDate );
        List<Map<String , Object >> mapList = new ArrayList<>();
        for ( GoodsReceiptLog goodsReceiptLog : goodsReceiptLogs ){
          Map<String , Object > map = new HashMap<>();
          String code = customFieldsAdviceService.existCustomValue( goodsReceiptLog.getItemBo() , Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL );

          map.put("handle",goodsReceiptLog.getHandle());
          map.put("machineType",goodsReceiptLog.getMachineType());
          map.put("itemBo",goodsReceiptLog.getItemBo());
          map.put("code",code );
          map.put("unit",goodsReceiptLog.getUnit());
          map.put("sfc",goodsReceiptLog.getSfc());
          map.put("storageLocation",goodsReceiptLog.getStorageLocation());
          map.put("status",goodsReceiptLog.getStatus());
          map.put("qty",goodsReceiptLog.getQty());
          map.put("documentNo",goodsReceiptLog.getDocumentNo());
          map.put("grType",goodsReceiptLog.getGrType() );
          map.put("createdDateTime",goodsReceiptLog.getCreatedDateTime());
          map.put("createUserBo",goodsReceiptLog.getCreateUserBo());

          mapList.add(map);
        }

        return mapList;
    }
}
